package com.tenebrous.jestic.service.framwork.system;

import com.tenebrous.jestic.entity.framwork.system.MetaDistrict;
import com.tenebrous.jestic.mapper.idao.framwork.system.MetaDistrictMapper;
import com.tenebrous.jestic.service.SuperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class MetaDistrictService extends SuperService<MetaDistrict> {

    @Autowired
    private MetaDistrictMapper mapper;

    @Override
    @Autowired
    protected void setSuperMapper() {
        super.setSuperMapper(this.mapper);
    }

    /**
     * 删除当前及子节点记录
     *
     * @param id
     * @return
     */
    @Transactional
    @CacheEvict(value = "spring_cache:portal:region", allEntries = true)
    public int deleteById(String id) {
        List<MetaDistrict> list = this.mapper.selectByPid(id);
        for (MetaDistrict it : list) {
            deleteById(it.getId());
            this.mapper.deleteByPrimaryKey(it.getId());
        }
        return this.mapper.deleteByPrimaryKey(id);
    }

    @Cacheable(cacheNames = "spring_cache:portal:region", keyGenerator = "cacheKeyGenerator")
    public MetaDistrict getByCode(String code) {
        return this.mapper.selectOneByCode(code);
    }

    @Cacheable(cacheNames = "spring_cache:portal:region", keyGenerator = "cacheKeyGenerator")
    public List<MetaDistrict> queryByPid(String pid) {
        return this.mapper.selectByPid(pid);
    }

    @Cacheable(cacheNames = "spring_cache:portal:region", keyGenerator = "cacheKeyGenerator")
    public List<MetaDistrict> queryByCode(String code) {
        MetaDistrict entity = this.mapper.selectOneByCode(code);
        if (entity != null) {
            return this.mapper.selectByPid(entity.getId());
        }
        return new ArrayList();
    }
}
